package com.gw.yunke.exception;



import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


@ControllerAdvice
public class GlobalExceptionHandler {

    private static Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ErrorInfo<String> jsonErrorhandler(HttpServletRequest request, Exception e) {

        log.error("请求 {} 发生异常 ：{}",request.getRequestURL(),e.getMessage());
        //e.printStackTrace();
        e.printStackTrace();
        ErrorInfo<String> errorInfo = new ErrorInfo<String>();
        if(e instanceof TokenTimeOutExeption){
            errorInfo.setCode(ErrorConstant.TOKENTIMEOUTERROR);
        }else if(e instanceof LoginInfoException){
            errorInfo.setCode(ErrorConstant.LOGININFOERROR);
        }else if(e instanceof UnLoginException){
            errorInfo.setCode(ErrorConstant.UNLOGINERROR);
        }
        else {
            errorInfo.setCode(ErrorConstant.DEFAUTLERROR);
        }
        errorInfo.setUrl(request.getRequestURL().toString());
        errorInfo.setMessage(e.getMessage());
        errorInfo.setData("Exception");
        return errorInfo;
    }
}
